﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace ConsoleApplication.Utils
{
    class HttpsUtil
    {
        /**
        * Creator JYW 2019-09-02
        * Description 后台接口调用公共方法
        */
        public static JObject Post(string code, JArray param, JArray session)
        {
            //System.Diagnostics.Debugger.Launch();
            string result = "";
            string ip = GetAppConfig("serverIP");
            string authorization = GetAppConfig("authorization");
            string contentType = GetAppConfig("Content-Type");
            string method = GetAppConfig("method");
            //if (session[0]["connetedIP"] != null)
            //{
            //    ip = session[0]["connetedIP"].ToString();
            //    ip = Regex.Replace(ip, "[$]", ":");
            //    authorization = session[0]["authorization"].ToString();
            //    authorization = Regex.Replace(authorization, "[$]", " ");
            //}
            ////Formal
            //ip = "http://172.16.2.131:52773/service/sysInternalMutiple";
            //Test
            ip = "https://np.h03.p0551.com/bdhealth/";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ip);
            method = "Post";
            request.Method = method;
            //Formal
            //request.Headers.Add("Authorization", "Basic cHJ5azpwcnlrQDIwMjA=");
            //Test
            request.Headers.Add("Authorization", "Basic cHJoaXA6cHJoaXBAMjAyMA==");
            request.ContentType = "application/json; charset=UTF-8";
            JObject Params = new JObject();
            //Params.Add("code", code);
            Params.Add("params", param);
            Params.Add("session", session);

            Params.Add("code", "06080004");
            //JObject newPar = new JObject();
            //newPar.Add("admIDs", "58^113^116^120^146^187^193^222^253^265^266^324^367^373^379^394^408^409^613^626^629^710^721^735^820^867^870^871^921^923^925^929^1058^1138^1160^1185^1460^1474^1569^1692^2160^2165^2169^2186^2213^2315^2322^2337^2338^2417^2604^2659^2726^2731^2862^2902^3096^3117^3138^3158^3212^3214^3267^3356^3827^3834^3860^3872^3898^3953^3981^4025^4083^4246^4301^4686^4739^5042^5069^5290^5305^5413^5535^5616^5618^5623^5656^5683^5798^5815^5832^5839^5900^6025^6028^6040^6068^6070^6087^6475^6504^6514^6530^6592^6615^6639^6666^6798^6866^6871^6880^6883^6955^7162^7273^7278^7427^7448^7753^7768^7778^7855^7945^7959^8170^8220^8241^8262^8803^9170^9469^9514^9538^9595^9615^9661^9674^9675^9730^9750^9803^9874^9901^9945^9989^10103^10426^10438^10447^10599^10627^10792^10940^10943^10949^10951^10966^10967^11042^11057^11062^11067^11079^11195^11202^11579^11588^11597^11621^11689^11699^12029^12090^12123^12135^12180^12236^12306^12557^12808^12966^13011^13326^13337^13427^13741^13756^13758^13823^13937^14021^14063^14185^14360^14377^14378^14390^14405^14408^14454^14459^14539^14781^14808^15029^15134^15138^15173^15283");
            //JArray parAry = new JArray();
            //parAry.Add(newPar);
            //Params.Add("params", parAry);
            //JObject newSession = new JObject();
            //newSession.Add("groupID", "3");
            //newSession.Add("hospCode", "H03");
            //newSession.Add("hospID", "25");
            //newSession.Add("ipv4", "192.168.5.176");
            //newSession.Add("locID", "1");
            //newSession.Add("sessionID", "rxHwoagmmJ");
            //newSession.Add("userID", "6");
            //JArray sessAry = new JArray();
            //sessAry.Add(newSession);
            //Params.Add("session", sessAry);
            string json = Params.ToString();
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }
            var httpResponse = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }

            JObject jsonResult = (JObject)JsonConvert.DeserializeObject(result.ToString());
            if (jsonResult["errorCode"].ToString() != "0")
            {
                Console.WriteLine("接口" + code + "访问异常" + "\n" + jsonResult["errorMessage"].ToString());
                Console.ReadKey();
                if ((jsonResult["errorCode"].ToString()) == "01040053")
                {
                    Environment.Exit(0);
                }
                return null;
            }
            return jsonResult;
        }
        /**
         * 获取系统配置
         */
        ///<summary> 
        ///返回appSettings配置节的value项  
        ///</summary> 
        ///<param name="strKey"></param> 
        ///<returns></returns> 
        public static string GetAppConfig(string strKey)
        {
            return ConfigurationManager.AppSettings[strKey];
        }
    }
}
